type Message = {
  id: number;
  type: MessageType;
  sendmessage: string;
};

enum MessageType {
  "Image" = "Image",
  "Audio" = "Audio",
  "Message" = "Message",
}

let message: Message[] = [
  {
    id: 1,
    type: MessageType.Message,
    sendmessage: "你好啊，今晚咱们一起出三里屯吧，",
  },
  {
    id: 2,
    type: MessageType.Message,
    sendmessage: "朝辞白帝彩云间，千里江陵一日还。",
  },
  {
    id: 3,
    type: MessageType.Audio,
    sendmessage: "你好！张无忌",
  },
  {
    id: 4,
    type: MessageType.Audio,
    sendmessage: "刘老根苦练舞台绝技！",
  },
  {
    id: 5,
    type: MessageType.Image,
    sendmessage: "今晚王牌对王牌节目咋样？",
  },
];

function searchMsg(
  condition: MessageType | number
): Message | undefined | Message[] {
  if (typeof condition === "number") {
    // 根据id查询
    return message.find((o) => o.id === condition);
  } else {
    // 根据type类型查找
    return message.filter((o) => o.type === condition);
  }
}

// searchMsg返回的是联合类型，不能直接点
/* (searchMsg(1) as Message)?.id
(searchMsg(MessageType.Image) as Message[]).forEach(element: Messsage => {
  console.log(element)
}); */

export {};
